package io.virtualapp.hook_sdk.app;

import android.content.Intent;
import android.text.TextUtils;
import android.util.Log;

import com.lody.virtual.client.core.VirtualCore;
import com.lody.virtual.client.ipc.VActivityManager;
import com.lody.virtual.remote.InstallResult;
import com.lody.virtual.remote.InstalledAppInfo;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import io.virtualapp.hook_sdk.dao.RiNotesService;
import io.virtualapp.hook_sdk.models.InstallAppBean;

public class InstallAppUtils {

    private int flags = 104;

    public boolean installApp(String sourcePath){
        if (TextUtils.isEmpty(sourcePath))
            return false;

        String path = sourcePath.substring(0,
                sourcePath.lastIndexOf("/"))+
                "/./"+sourcePath.substring(sourcePath.lastIndexOf("/")+1);
        Log.e("ZXY","sourcePath:"+sourcePath);
        List<InstalledAppInfo> datas = VirtualCore.get().getInstalledApps(flags);
        Log.e("ZXY","sourcePath:"+(datas==null));
        for (InstalledAppInfo info : datas){
            if (info.apkPath.equals(sourcePath))
                return true;
        }
        InstallResult result = VirtualCore.get().installPackage(path,flags);
        if (result.isSuccess) {
            InstallAppBean bean = new InstallAppBean();
            bean.app_time = System.currentTimeMillis();
            bean.apk_path = sourcePath;
            bean.app_name = result.packageName;
            RiNotesService.getInstance().insertApp(bean);
        }
        return result.isSuccess;
    }
    public boolean installApps(ArrayList<String> sourcePaths){
        if (sourcePaths==null)
            return false;
        if (sourcePaths.size()==0)
            return false;
        boolean[]results = new boolean[sourcePaths.size()];
        for (int i = 0;i < sourcePaths.size();i ++){
            results[i] = installApp(sourcePaths.get(i));
        }
        boolean flag = true;
        for (boolean b:results){
            if (!b) {
                flag = false;
                break;
            }
        }
        if (!flag)
            clear();
        return flag;
    }

    private void clear(){
        List<InstalledAppInfo> datas =  VirtualCore.get().getInstalledApps(flags);
        for (InstalledAppInfo info : datas){
            VirtualCore.get().uninstallPackage(info.packageName);
        }
    }

    public boolean unInstallApp(String packageName){
        if (TextUtils.isEmpty(packageName))
            return false;
        InstalledAppInfo info = VirtualCore.get().getInstalledAppInfo(packageName,flags);
        if (info==null)
            return true;
        boolean flag = VirtualCore.get().uninstallPackage(packageName);
        RiNotesService.getInstance().delApp(packageName);
        return flag;
    }
    public boolean unInstallApps(ArrayList<String>packageNames){
        if (packageNames == null)
            return false;
        if (packageNames.size() == 0)
            return false;
        boolean[]results = new boolean[packageNames.size()];
        for (int i = 0;i < packageNames.size();i ++){
            results[i] = unInstallApp(packageNames.get(i));
        }
        boolean flag = true;
        for (boolean b : results){
            flag = b;
            if (!b)
                break;
        }
        return flag;
    }

    public boolean launchApp(String packageName){
        if (TextUtils.isEmpty(packageName))
            return false;
        InstalledAppInfo info = VirtualCore.get().getInstalledAppInfo(packageName,flags);
        if (info==null)
            return false;
        try {
            Intent intent = VirtualCore.get().getLaunchIntent(packageName,0);
            VirtualCore.get().preOpt(packageName);
            VActivityManager.get().startActivity(intent,0);
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }
    public List<InstalledAppInfo> selInstalledApps(){
        return VirtualCore.get().getInstalledApps(flags);
    }
}
